<?php
namespace app\index\service;


use app\index\controller\Flow;
use app\index\model\ArchiveModel;
use app\index\model\BidModel;
use app\index\model\CertBorrowModel;
use app\index\model\CertModel;
use app\index\model\CertUseModel;
use app\index\model\ContractCaiModel;
use app\index\model\ContractNeiModel;
use app\index\model\ContractShiModel;
use app\index\model\DangJieListModel;
use app\index\model\DangModel;
use app\index\model\DeptModel;
use app\index\model\EnginBondShouModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginModel;
use app\index\model\FileModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowClassModel;
use app\index\model\FlowCourseModel;
use app\index\model\FlowNodeModel;
use app\index\model\FlowPromerModel;
use app\index\model\FlowSetModel;
use app\index\model\FlowShenlogModel;
use app\index\model\FlowTalkModel;
use app\index\model\FlowTaskModel;
use app\index\model\FlowTaskShenModel;
use app\index\model\PaymentJieModel;
use app\index\model\ReadsModel;
use app\index\model\SealGaiListModel;
use app\index\model\UserGangModel;
use app\index\model\UserModel;
use app\index\model\UserRoleModel;
use app\index\relevan\CertRelevan;
use app\index\relevan\EnginRelevan;
use app\index\relevan\OtherRelevan;
use think\Db;
use think\Exception;

class FlowService extends CommonService
{

    public function get_flow_status(){
        return [
            0 => '进行中',
            1 => '审核完成（同意）',
            2 => '审核完成（不同意）',
            3 => '审核完成（超管结束）',
            5 => '审核完成（作废）',
        ];
    }

    public function get_flow_modename(){
        $list = [];

        $set = FlowSetModel::field('*,id as value')->select()->toArray();
        $nav = (new UserService())->get_flowset_nav();
        foreach ($nav as $k=>&$v){
            $children = [];
            foreach ($set as $vv){
                if($vv['typename'] == $k){
                    $children[] = $vv;
                }
            }
            if($children){
                $list[] = ['name'=>$v, 'children'=>$children,'disabled'=>true];
            }
        }
        return $list;
    }

    public function save_bill($modenum, $mid){
        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) throw new Exception("操作失败");

        $minfo = Db::name($set['modenum'])->where(['id'=>$mid])->find();
        if(!$minfo) throw new Exception("操作失败");
        $minfo['created_user_name'] = UserModel::get_realname_by_pk($minfo['created_user']);


        $bill = FlowBillModel::where(['modeid'=>$set['id'],'mid'=>$mid])->find();


        if(!$bill){
            $bill = $this->create_flow_bill($minfo, $set);
        }else{
            FlowBillModel::where(['id'=>$bill['id']])->update([
                'isturn' => $minfo['isturn'],
                'flowrelas' => $minfo['flowrelas'],
                'advaners' => $minfo['advaners'],
                'summary' => $this->create_flow_summary($set,$minfo),
                'bidid' => $this->create_flow_bidid($set['modenum'],$minfo),
                'enginid' => $this->create_flow_enginid($set['modenum'],$minfo),
                'enginame' => $this->create_flow_enginame($set['modenum'],$minfo),
                'created_at' => date("Y-m-d H:i:s"),
                'updated_at' => date("Y-m-d H:i:s"),
            ]);

            $bill = FlowBillModel::where(['id'=>$bill['id']])->find();
        }

        $bill['created_user_name'] = UserModel::get_realname_by_pk($bill['created_user']);

        Db::name($set['modenum'])->where(['id'=>$mid])->update(['billid'=>$bill['id']]);

        if($minfo['isturn'] == FlowbillModel::ISTURN_OFF){
            return true;
        }else{
            (new OptService())->add_flow_shenlog(['billid'=>$bill['id'],'name'=>'发起流程'], '提交申请',$this->userinfo['realname'], '', '','');
        }

        //判断流程模块是否开启
        $classid = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id'],'status'=>1])->value('id');
        if(!$classid) throw new Exception("操作失败,流程未开启");

        //判断流程线是否有已发布的
        $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$classid,'status'=>1,'deleted_user'=>0])->find();
        if(!$course) throw new Exception("操作失败,流程未设置");

        $step = json_decode($course['step'], true);
        if(!is_array($step)) throw new Exception("操作失败,流程未设置");
        FlowBillModel::where(['id'=>$bill['id']])->update(['step'=>$course['step']]);


        //删除 历史 审批节点
        FlowNodeModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->delete();
        //生成预审人 - 以审核节点 形式处理
        if(isset($step[0]['data']['isadvan']) && $step[0]['data']['isadvan'] > 0 && $bill['advaners']){
            $this->create_flow_advaners_node($bill);
        }


        if(isset($step[0]['data']['coursetype']) && $step[0]['data']['coursetype'] > 0){
            if(!$minfo['nexter']){
                throw new Exception("操作失败,请选择指定下一步审核人");
            }
            $nexterInfo = UserModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'id'=>$minfo['nexter']])->find();
            if(!$nexterInfo){
                throw new Exception("操作失败,当前选择的下一步审核人不存在，请重新选择");
            }

            FlowBillModel::where(['id'=>$bill['id']])->update(['coursetype'=>1]);
        }

        //判断可建对象
        if(!$this->check_course_start_users($step)){
            throw new Exception("操作失败,您没有当前流程发起权限，请联系管理员");
        }


        //解析生成审批节点
        $this->create_flow_node($step, $minfo, $bill);
        $node = FlowNodeModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->select()->toArray();
        if(isset($step[0]['data']['isadvan']) && $step[0]['data']['isadvan'] > 0 && $bill['advaners']){
            if(count($node) < 2){
                throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
            }
        }else{
            if(!$node){
                throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
            }
        }

        //生成审核任务
        $this->create_flow_node_task($node[0], $minfo, $bill);

        return true;
    }

    public function save_bill_shen($modenum, $mid){
        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) throw new Exception("操作失败");


        $minfo = Db::name($set['modenum'])->where(['id'=>$mid])->find();
        if(!$minfo) throw new Exception("操作失败");
        $minfo['created_user_name'] = UserModel::get_realname_by_pk($minfo['created_user']);

        $bill = FlowBillModel::where(['modeid'=>$set['id'],'mid'=>$mid])->find();
        if(!$bill){
            $bill = $this->create_flow_bill($minfo, $set);
        }else{
            FlowBillModel::where(['id'=>$bill['id']])->update([
                'flowrelas' => $minfo['flowrelas'],
                'advaners' => $minfo['advaners'],
                'summary' => $this->create_flow_summary($set, $minfo),
                'bidid' => $this->create_flow_bidid($set['modenum'], $minfo),
                'enginid' => $this->create_flow_enginid($set['modenum'], $minfo),
                'enginame' => $this->create_flow_enginame($set['modenum'], $minfo),
            ]);
            $bill = FlowBillModel::where(['id'=>$bill['id']])->find();
        }
        $bill['created_user_name'] = UserModel::get_realname_by_pk($bill['created_user']);

        Db::name($set['modenum'])->where(['id'=>$mid])->update(['billid'=>$bill['id']]);

        $myTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'state'=>1,'opt'=>'tui','nodeid'=>0,'tuiid'=>0])
            ->where(['uid'=>$this->userinfo['id'],'deleted_user'=>0])->find();

        if(!$myTask) throw new Exception("操作失败");

        FlowTaskModel::where(['id'=>$myTask['id']])->update(['state'=>2, 'deleted_user'=>1]);

        if($myTask['tiaotype']  == 2){
            FlowTaskModel::where('id','in',$myTask['tingids'])->update(['state'=>1]);
        }else{
            //删除 历史 审批节点
            FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->update(['deleted_user'=>1]);
            FlowNodeModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->delete();

            //解析生成审批节点
            $step = json_decode($bill['step'], true);

            //生成预审人 - 以审核节点 形式处理
            if(isset($step[0]['data']['isadvan']) && $step[0]['data']['isadvan'] && $bill['advaners']){
                $this->create_flow_advaners_node($bill);
            }


            $this->create_flow_node($step, $minfo, $bill);
            $node = FlowNodeModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->select()->toArray();
            if(isset($step[0]['data']['isadvan']) && $step[0]['data']['isadvan'] && $bill['advaners']){
                if(count($node) < 2){
                    throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
                }
            }else{
                if(!$node){
                    throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
                }
            }
            //生成审核任务
            $this->create_flow_node_task($node[0], $minfo, $bill);

        }
        FlowBillModel::where(['id'=>$bill['id']])->update(['recall'=>0]);
        FlowTaskShenModel::where(['billid'=>$bill['id']])->update(['recall'=>0]);
        (new OptService())->add_flow_shenlog($myTask, '申请人发起申请',$this->userinfo['realname'], '', '','');
        return true;
    }






    public function create_flow_node_task($data, $minfo, $bill){
        if($data['shentype'] != 'user'){
            $data['plain'] = 0;
        }
        $userArr = $this->get_flow_node_user($data, $minfo, $bill);

        FlowNodeModel::where(['id'=>$data['id']])->update(['final'=>implode(',', $userArr)]);


        $isadd = 1;
        if($data['signtype'] > 1){
            $isadd = 0;
        }

        if(!$userArr){
            $userArr = [0];
        }

        foreach ($userArr as $key=>$uid){
            $state = 1;
            if($data['plain'] > 0){
                if($key > 0){
                    $state = 0;
                }
            }

            FlowTaskModel::insert([
                'comid' => $this->userinfo['comid'],
                'billid' => $bill['id'],
                'nodeid' => $data['id'],
                'name' => $data['name'],
                'signtype' => $data['signtype'],
                'plain' => $data['plain'],
                'uid' => $uid,
                'inituid' => $uid,
                'isadd' => $isadd,
                'rovedit' => isset($data['rovedit']) ? $data['rovedit'] : 0,
                'state' => $state,
                'created_at' => date("Y-m-d H:i:s"),
            ]);

            if($state == 1){
                (new WorkerService())->add_worker_bill($uid, 'shen', $bill);
            }
        }
        return true;
    }


    public function get_flow_node_user($data, $minfo, $bill){
        $userArr = [];
        if($data['shentype'] == 'user'){
            foreach (explode(',', $data['users']) as $_userid){
                $userArr[] = UserModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'id'=>$_userid])->value('id');
            }
        }elseif($data['shentype'] == 'dept'){
//            $deptArr = DeptModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1])
//                ->where('id','in', $data['users'])->column('id');
//            $userArr = UserModel::where(['comid'=>$this->userinfo['comid'], 'deleted_user'=>0, 'status'=>1])
//                ->where('deptid','in', $deptArr)->group('id')->column('id');

            $fuerArr = DeptModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1])
                ->where('id','in', $data['users'])->column('fuer');
            $userArr = UserModel::where(['comid'=>$this->userinfo['comid'], 'deleted_user'=>0, 'status'=>1])
                ->where('id','in', $fuerArr)->group('id')->column('id');
        }elseif($data['shentype'] == 'role'){
            $roleArr = UserRoleModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0])
                ->where('id','in', $data['users'])->column('id');
            foreach ($roleArr as $_roleid){
                $arr = UserModel::where(['comid'=>$this->userinfo['comid'], 'deleted_user'=>0, 'status'=>1])
                    ->where(Db::raw("find_in_set(".$_roleid.",roleid)"))->column('id');
                $userArr = array_merge($userArr, $arr);
            }
        }elseif($data['shentype'] == 'gang'){
            $gangArr = UserGangModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1])
                ->where('id','in', $data['users'])->column('id');
            foreach ($gangArr as $_gangid){
                $arr = UserModel::where(['comid'=>$this->userinfo['comid'], 'deleted_user'=>0, 'status'=>1])
                    ->where(Db::raw("find_in_set(".$_gangid.",gangid)"))->column('id');
                $userArr = array_merge($userArr, $arr);
            }
        }elseif($data['shentype'] == 'base'){
            $userArr = $this->get_flow_node_base_user($data, $minfo, $bill);
        }

        return array_unique($userArr);
    }


    public function get_flow_node_base_user($data, $minfo, $bill){
        $userArr = [];

        if($data['users'] == 'creater'){
            $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$bill['created_user']])->value('id');
        }

        if($data['users'] == 'credepter'){
            $credeptId = UserModel::where(['id'=>$bill['created_user']]) ->value('deptid');
            $fuer = DeptModel::where(['id'=>$credeptId])->value('fuer');
            $credepter = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$fuer])->value('id');
            if($credepter){
                $userArr[] = $credepter;
            }
        }

        if($data['users'] == 'fuzer'){
            if($bill['modenum'] == 'bid'){
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$minfo['fuzer']])->value('id');
            }
            if($bill['modenum'] == 'bid_book'){
                $fuzer = BidModel::where(['id'=>$minfo['bidid']])->value('fuzer');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$fuzer])->value('id');
            }
            if($bill['modenum'] == 'engin_bond_jiao'){
                $fuzer = BidModel::where(['id'=>$minfo['bidid']])->value('fuzer');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$fuzer])->value('id');
            }
            if($bill['modenum'] == 'engin_bond_fan'){
                $bidid = EnginBondShouModel::where(['id'=>$minfo['shouid']])->value('bidid');
                $fuzer = BidModel::where(['id'=>$bidid])->value('fuzer');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$fuzer])->value('id');
            }
            if($bill['modenum'] == 'engin_bond_kai'){
                $fuzer = BidModel::where(['id'=>$minfo['bidid']])->value('fuzer');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$fuzer])->value('id');
            }
        }

        if($data['users'] == 'booker'){
            if($bill['modenum'] == 'bid'){
                foreach (explode(',', $minfo['booker']) as $_userid){
                    $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$_userid])->value('id');
                }
            }
            if($bill['modenum'] == 'bid_book'){
                $booker = BidModel::where(['id'=>$minfo['bidid']])->value('booker');
                foreach (explode(',', $booker) as $_userid){
                    $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$_userid])->value('id');
                }
            }
            if($bill['modenum'] == 'engin_bond_jiao'){
                $booker = BidModel::where(['id'=>$minfo['bidid']])->value('booker');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$booker])->value('id');
            }
            if($bill['modenum'] == 'engin_bond_fan'){
                $bidid = EnginBondShouModel::where(['id'=>$minfo['shouid']])->value('bidid');
                $booker = BidModel::where(['id'=>$bidid])->value('booker');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$booker])->value('id');
            }
            if($bill['modenum'] == 'engin_bond_kai'){
                $booker = BidModel::where(['id'=>$minfo['bidid']])->value('booker');
                $userArr[] = UserModel::where(['deleted_user'=>0,'status'=>1,'id'=>$booker])->value('id');
            }
        }


        if(in_array($data['users'], ['man_xiang','man_zhuan','man_zi','man_cheng','man_zhian'])){
            $users = '';
            if($bill['modenum'] == 'engin'){
                $users = $minfo[$data['users']];
            }
            if($bill['modenum'] == 'engin_junyan'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_junjie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'contract_shi'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'contract_bu'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'contract_cai'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'contract_bian'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_budget'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_budbian'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'stuff_cai'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'stuff_ru'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'stuff_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'labour_shen'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'labour_ling'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'labour_yong'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'labour_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'device_jin'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'device_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'device_lin'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'subbao_shen'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'subbao_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_diary'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_speed'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_chan'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_stamp'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'visa'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'payment_bao'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'payment_jie'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'income_huan'){
                $enginid = PaymentJieModel::where(['id'=>$minfo['jieid']])->value('enginid');
                $users = EnginModel::where(['id'=>$enginid])->value($data['users']);
            }
            if($bill['modenum'] == 'piao_kai'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'piao_tax'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'payment_neifu'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'payment_engin'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'cert_use_bid'){
                $users = BidModel::where(['id'=>$minfo['bidid']])->value($data['users']);
            }
            if($bill['modenum'] == 'cert_use'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_bond_jiao'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_bond_fan'){
                $enginid = EnginBondShouModel::where(['id'=>$minfo['shouid']])->value('enginid');
                $users = EnginModel::where(['id'=>$enginid])->value($data['users']);
            }
            if($bill['modenum'] == 'engin_bond_kai'){
                $users = EnginModel::where(['id'=>$minfo['enginid']])->value($data['users']);
            }

            $userArr = UserModel::where(['deleted_user'=>0,'status'=>1])->where('id','in',$users)->column('id');
        }
        return $userArr;
    }


    public function create_flow_node($data, $minfo, $bill){
        foreach ($data as $v){
            if($v['node'] == 'start'){
                if(isset($v['data']['coursetype']) && $v['data']['coursetype'] > 0){
                    $node[] = $this->get_peizhi_start_info($minfo, $bill);
                    break;
                }
            }

            if($v['node'] == 'rover'){
                $node[] = $this->get_peizhi_rover_info($v['data'], $bill);
            }

            if($v['node'] == 'term'){
                $termInfo = $this->get_peizhi_term_info($v['data'], $minfo, $bill);
                if(is_array($termInfo) && count($termInfo) > 0){
                    $this->create_flow_node($termInfo, $minfo, $bill);
                }
            }
        }
        return true;
    }

    public function get_peizhi_term_info($data, $minfo, $bill){
        $level = array_column($data,'level');
        array_multisort($level,SORT_ASC, $data);

        $minfo['created_user_old'] = $minfo['created_user'];
        $minfo['created_user'] = UserModel::get_realname_by_pk($minfo['created_user']);
        $minfo['created_dept'] = DeptModel::get_name_by_pk($minfo['deptid']);
        $factorObj = new FactorService();

        $res = false;
        $arrkey = 0;
        foreach ($data as $key => $item){
            if(!$item['list']){
                $res = true;
                $arrkey = $key;
                break;
            }else{
                $checkRes = call_user_func(array($factorObj, $bill['modenum']), $item['list'], $minfo);
                if($checkRes){
                    $res = true;
                    $arrkey = $key;
                    break;
                }
            }
        }

        if(!$res){
            throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
        }
        return $data[$key]['children'];
    }

    public function get_peizhi_start_info($minfo, $bill){
        $info = [];
        $info['name'] = '自由流程';
        $info['signtype'] = 1;
        $info['shentype'] = 'user';
        $info['users'] = $minfo['nexter'];
        $info['rovedit'] = 0;
        $info['plain'] = 0;
        $info['comid'] = $this->userinfo['comid'];
        $info['billid'] = $bill['id'];
        $info['type'] = 2;
        return FlowNodeModel::insert($info);
    }


    public function get_peizhi_rover_info($data, $bill){
        $info = [];
        if(!is_array($data)){
            throw new Exception("操作失败,系统未找到对应流程，请联系管理员。");
        }
        $info['name'] = $data['rovname'];
        $info['signtype'] = $data['signtype'];
        $info['shentype'] = $data['shentype'];
        $info['users'] = $data['users'];
        $info['rovedit'] = isset($data['rovedit']) ? $data['rovedit'] : 0;
        $info['plain'] = $data['plain'];
        $info['comid'] = $this->userinfo['comid'];
        $info['billid'] = $bill['id'];
        $info['type'] = 1;
        return FlowNodeModel::insert($info);
    }


    public function create_flow_advaners_node($bill){
        $info = [];
        $info['name'] = '预审';
        $info['signtype'] = 1;
        $info['shentype'] = 'user';
        $info['users'] = $bill['advaners'];
        $info['rovedit'] = 0;
        $info['plain'] = 0;
        $info['comid'] = $this->userinfo['comid'];
        $info['billid'] = $bill['id'];
        $info['type'] = 0;
        return FlowNodeModel::insert($info);
    }

    public function create_flow_bill($minfo, $set){
        $ins['comid'] = $minfo['comid'];
        $ins['chanid'] = $minfo['chanid'];
        $ins['deptid'] = $minfo['deptid'];
        $ins['flowuser'] = $minfo['flowuser'];
        $ins['flowdept'] = $minfo['flowdept'];
        $ins['flowrelas'] = $minfo['flowrelas'];
        $ins['advaners'] = $minfo['advaners'];
        $ins['sericnum'] = $this->create_flow_sericnum();
        $ins['bidid'] = $this->create_flow_bidid($set['modenum'], $minfo);
        $ins['enginid'] = $this->create_flow_enginid($set['modenum'], $minfo);
        $ins['enginame'] = $this->create_flow_enginame($set['modenum'], $minfo);
        $ins['summary'] = $this->create_flow_summary($set, $minfo);
        $ins['isturn'] = $minfo['isturn'];
        $ins['modeid'] = $set['id'];
        $ins['modenum'] = $set['modenum'];
        $ins['modename'] = $set['name'];
        $ins['mid'] = $minfo['id'];
        $ins['created_at'] = $minfo['created_at'];
        $ins['created_user'] = $this->userinfo['id'];
        $billid = FlowBillModel::insertGetId($ins);
        return FlowBillModel::where(['id'=>$billid])->find();
    }


    public function check_course_start_users($data){
        $shentype = isset($data[0]['data']['shentype']) ? $data[0]['data']['shentype'] : '';
        $users = isset($data[0]['data']['users']) ? $data[0]['data']['users'] : '';
        if(empty($shentype) || empty($users)){
            return true;
        }

        if($shentype == 'user'){
            if(in_array($this->userinfo['id'], explode(',', $users))){
                return true;
            }
        }
        if($shentype == 'dept'){
            if(in_array($this->userinfo['deptid'], explode(',', $users))){
                return true;
            }
        }
        if($shentype == 'role'){
            $userRole = explode(',', $this->userinfo['roleid']);
            $usersArr = explode(',', $users);
            foreach ($userRole as $v){
                if(in_array($v, $usersArr)){
                    return true;
                }
            }
        }
        if($shentype == 'gang'){
            $userGang = explode(',', $this->userinfo['gangid']);
            $usersArr = explode(',', $users);
            foreach ($userGang as $v){
                if(in_array($v, $usersArr)){
                    return true;
                }
            }
        }

        return false;
    }


    public function create_flow_sericnum(){
        $max = (new FlowBillModel())->get_max_sericnum($this->userinfo['comid']);
        if(!$max){
            return substr(date('Ymd'),2).'000001';
        }
        return $max+1;
    }
    public function create_flow_summary($set, $minfo){
        $str = '';

        // [流程名称] - [项目名称]
        if($set['modenum'] == 'bid'){
            $str = $set['name'].' - '.$minfo['name'];
        }
        // [流程名称] - [项目名称]
        if($set['modenum'] == 'bid_book'){
            $bid = BidModel::get_info_by_pk($minfo['bidid']);
            $str = $set['name'].' - '.$bid['name'];
        }
        // [流程名称] - [项目名称]
        if($set['modenum'] == 'bid_grant_book'){
            $bid = BidModel::get_info_by_pk($minfo['bidid']);
            $str = $set['name'].' - '.$bid['name'];
        }


        // [申请人]的[流程名称] - [单位名称] - [所属分类]
        if($set['modenum'] == 'dealcom'){
            $type = (new OptService())->get_base_showname('cai_dealcom_type', $minfo['type']);
            $str = $minfo['created_user_name'].' - '.$set['name'].' - '.$minfo['name'].' - '.$type;
        }
        // [申请人]的[流程名称] - [单位名称]
        if($set['modenum'] == 'dealcom_geng'){
            $str = $minfo['created_user_name'].' - '.$set['name'].' - '.$minfo['name'];
        }
        // [流程名称] - [项目名称] - [请款金额]  |  [流程名称] - [项目名称] - [保证金类型] - [请款金额]
        if($set['modenum'] == 'engin_bond_jiao'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $bondtype = (new PaymentService())->get_engin_bondtype_name($minfo['bondtype']);
            // $str = $set['name'].' - '.$engin['name'].' - '.$bondtype.' - '.$minfo['money'];
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [请款金额]  |  [流程名称] - [项目名称] - [保证金类型] - [付款对象] - [请款金额]
        if($set['modenum'] == 'engin_bond_fan'){
            $shou = EnginBondShouModel::where(['id'=>$minfo['shouid']])->find();
            $engin = EnginModel::get_info_by_pk($shou['enginid']);
            $bondtype = (new PaymentService())->get_engin_bondtype_name($shou['bondtype']);
            // $str = $set['name'].' - '.$engin['name'].' - '.$bondtype.' - '.$minfo['gonger'].' - '.$minfo['money'];
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [保函金额]
        if($set['modenum'] == 'engin_bond_kai'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['money'];
        }
        // [申请人]的[流程名称] - [款项摘要] - [报销金额]
        if($set['modenum'] == 'payment_gong'){
            $str = $minfo['created_user_name'].' - '.$set['name'].' - '.$minfo['zhai'].' - '.$minfo['money'];
        }
        // [申请人]的[流程名称] - [项目名称] - [款项摘要] - [报销金额]
        if($set['modenum'] == 'payment_bao'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$engin['name'].' - '.$minfo['zhai'].' - '.$minfo['money'];
        }
        // [申请人]的[流程名称] - [用途] - [借支金额]
        if($set['modenum'] == 'payment_jie'){
            $incyong = (new OptService())->get_base_showname('cai_income_incyong', $minfo['incyong']);
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$incyong.' - '.$minfo['money'];
        }
        // [申请人]的[流程名称] - [还款说明] - [还款金额]
        if($set['modenum'] == 'income_huan'){
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$minfo['zhai'].' - '.$minfo['money'];
        }
        // [申请人]的[流程名称] - [项目名称] - [发票类型] - [开票金额]
        if($set['modenum'] == 'piao_kai'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $piaotype = (new PiaoService())->get_shou_piaotype_name($minfo['piaotype']);
            $str = $minfo['created_user_name'].' - '.$set['name'].' - '.$engin['name'].' - '.$piaotype.' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [预缴月份] - [预缴金额]
        if($set['modenum'] == 'piao_tax'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['month'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [内包责任人] - [请款金额]
        if($set['modenum'] == 'payment_neifu'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $neiid = ContractShiModel::where(['deleted_user'=>0,'enginid'=>$minfo['id'],'isturn'=>1,'status'=>1])->value('neiid');
            $dutyer = ContractNeiModel::where(['id'=>$neiid])->value('dutyer');
            $str = $set['name'].' - '.$engin['name'].' - '.$dutyer.' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [款项类型] - [供应方] - [请款金额]
        if($set['modenum'] == 'payment_engin'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $data['data']['budid_name'] = (new PaymentService())->get_payment_budid_name($minfo['budid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$data['data']['budid_name'].' - '.$minfo['gonger'].' - '.$minfo['money'];
        }
        // [申请人]的[流程名称] - [款项摘要] - [请款金额]
        if($set['modenum'] == 'payment_day'){
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$minfo['zhai'].' - '.$minfo['money'];
        }
        // [申请人]的[流程名称]
        if($set['modenum'] == 'cert_borrow'){
            $str = $minfo['created_user_name'].'的'.$set['name'];
        }
        // [申请人]的[流程名称]
        if($set['modenum'] == 'cert_down'){
            $str = $minfo['created_user_name'].'的'.$set['name'];
        }
        // [流程名称] - [项目名称] - [申请类型]
        if($set['modenum'] == 'cert_use'){
            $engin_name = EnginModel::where('id',$minfo['enginid'])->value('name');
            $str = $set['name'].' - '.$engin_name;
        }
        if($set['modenum'] == 'cert_use_bid'){
            $engin_name = BidModel::where('id',$minfo['bidid'])->value('name');
            $str = $set['name'].' - '.$engin_name;
        }


        if($set['modenum'] == 'engin'){
            $businessName = $minfo['business'] == 1 ? '自营' : '联营';
            $str = $set['name'].' - '.$minfo['name'].' - '.$businessName;
        }

        // 项目名称 - 竣工验收单 - 验收日期
        if($set['modenum'] == 'engin_junyan'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $engin['name'].' - 竣工验收单 - '.$minfo['yandt'];
        }
        // 项项目名称 - 竣工结算单 - 审定金额
        if($set['modenum'] == 'engin_junjie'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $engin['name'].' - 竣工结算单 - '.$minfo['money'];
        }

        // [流程名称] - [项目名称] - [合同金额]
        if($set['modenum'] == 'contract_shi'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [补充合同金额]
        if($set['modenum'] == 'contract_bu'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [供应方] - [合同类型] - [合同金额]
        if($set['modenum'] == 'contract_cai'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $caihetypeName = (new ContractService())->get_contract_caihetype_name($minfo['caihetype']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['gonger'].' - '.$caihetypeName.' - '.$minfo['money'].']';
        }
        // [流程名称] - [项目名称] - [供应方] - [合同类型] - [变更金额]
        if($set['modenum'] == 'contract_bian'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $cai = ContractCaiModel::get_info_by_pk($minfo['caiid']);
            $caihetypeName = (new ContractService())->get_contract_caihetype_name($cai['caihetype']);
            $str = $set['name'].' - '.$engin['name'].' - '.$cai['gonger'].' - '.$caihetypeName.' - '.$minfo['bianfee'].']';
        }
        // [流程名称] - [项目名称] - [预算主题]
        if($set['modenum'] == 'engin_budget'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['name'];
        }
        // [流程名称] - [项目名称] - [预算主题] - [变更类型]
        if($set['modenum'] == 'engin_budbian'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $name = EnginBudgetModel::where('id',$minfo['budgetid'])->value('name');
            $type = '';
            if($minfo['type'] == 1){
                $type = '调整';
            }else if($minfo['type'] == 2){
                $type = '增项';
            }else if($minfo['type'] == 3){
                $type = '调整及增项';
            }
            $str = $set['name'].' - '.$engin['name'].' - '.$name.' - '.$type;
        }
        // [流程名称] - [协议名称] - [协议类型] - [内包责任人]
        if($set['modenum'] == 'contract_nei'){
            $neihetype = $minfo['neihetype'] == 1 ? '年度合作协议' : '单项目合作协议';
            $str = $set['name'].' - '.$minfo['name'].' - '.$neihetype.' - '.$minfo['dutyer'];
        }


        // [流程名称] - [项目名称] - [供应商] - [入库金额]
        if($set['modenum'] == 'stuff_cai'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['gonger'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [供应商] - [入库金额]
        if($set['modenum'] == 'stuff_ru'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['gonger'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [供应商] - [结算金额]
        if($set['modenum'] == 'stuff_jie'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['gonger'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [劳务班组] - [核定金额]
        if($set['modenum'] == 'labour_shen'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $cai = ContractCaiModel::get_info_by_pk($minfo['caiid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$cai['gonger'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [劳务班组] - [登记金额]
        if($set['modenum'] == 'labour_ling'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['gonger'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [劳务班组] - [登记金额]
        if($set['modenum'] == 'labour_yong'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['yongdt'].' - '.$minfo['yongxing'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [劳务班组] - [结算金额]
        if($set['modenum'] == 'labour_jie'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $cai = ContractCaiModel::get_info_by_pk($minfo['caiid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$cai['gonger'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [供应商]
        if($set['modenum'] == 'device_jin'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $cai = ContractCaiModel::get_info_by_pk($minfo['caiid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$cai['gonger'];
        }
        // [流程名称] - [项目名称] - [供应商] - [结算金额]
        if($set['modenum'] == 'device_jie'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $cai = ContractCaiModel::get_info_by_pk($minfo['caiid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$cai['gonger'].' - '.$minfo['money'];
        }
        // [流程名称] - [项目名称] - [供应商] - [登记金额]
        if($set['modenum'] == 'device_lin'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['gonger'].' - '.$minfo['money'];
        }

        // [流程名称] - [项目名称] - [分包单位] - [核定金额]
        if($set['modenum'] == 'subbao_shen'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $cai = ContractCaiModel::get_info_by_pk($minfo['caiid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$cai['gonger'].' - '.$minfo['money'];
        }

        // [流程名称] - [项目名称] - [分包单位] - [结算金额]
        if($set['modenum'] == 'subbao_jie'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $cai = ContractCaiModel::get_info_by_pk($minfo['caiid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$cai['gonger'].' - '.$minfo['money'];
        }

        // [流程名称] - [项目名称] - [日期]
        if($set['modenum'] == 'engin_diary'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['dt'];
        }

        // [流程名称] - [项目名称] - [填报月份] - [累计完成进度%]
        if($set['modenum'] == 'engin_speed'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['month'];
        }

        // [流程名称] - [项目名称] - [填报月份] - [累计完成产值元]
        if($set['modenum'] == 'engin_chan'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $str = $set['name'].' - '.$engin['name'].' - '.$minfo['month'].' - '.$minfo['leijin'];
        }

        // [流程名称] - [项目名称] - [资料类型]
        if($set['modenum'] == 'engin_stamp'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $zitype = (new OptService())->get_base_showname('scene_zitype', $minfo['zitype']);
            $str = $set['name'].' - '.$engin['name'].' - '.$zitype;
        }
        // [流程名称] - [项目名称] - [签证类别] - [涉及金额]
        if($set['modenum'] == 'visa'){
            $engin = EnginModel::get_info_by_pk($minfo['enginid']);
            $visatype = (new OptService())->get_base_showname('scene_visatype', $minfo['visatype']);
            $str = $set['name'].' - '.$engin['name'].' - '.$visatype.' - '.$minfo['shemoney'];
        }
        // [申请人]的[流程名称] - [公文类型] - [标题]
        if($set['modenum'] == 'wen_ban'){
            $wentype = (new OptService())->get_base_showname('qi_wen_wentype', $minfo['wentype']);
            $str = '['.$minfo['created_user_name'].']的['.$set['name'].'] - ['.$wentype.'] - ['.$minfo['title'].']';
        }

        // [申请人]的[流程名称] - [采购主题]
        if($set['modenum'] == 'wu_cai'){
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$minfo['title'];
        }

        // [申请人]的[流程名称] - [领用主题]
        if($set['modenum'] == 'wu_ling'){
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$minfo['title'];
        }

        // [申请人]的[流程名称] - [资料文件名称]等
        if($set['modenum'] == 'seal_gai'){
            $oneName = SealGaiListModel::where(['gaiid'=>$minfo['id']])->value('name');
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$oneName.'等';
        }

        // [申请人]的[流程名称] - [档案文件名称]等
        if($set['modenum'] == 'dang_jie'){
            $oneDangid = DangJieListModel::where(['jieid'=>$minfo['id']])->value('dangid');
            $oneName = DangModel::where(['id'=>$oneDangid])->value('name');
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$oneName.'等';
        }

        // [申请人]的[流程名称] - [请假类型][天数]天
        if($set['modenum'] == 'kao_jia'){
            $jiatype = (new OptService())->get_base_showname('qi_kao_jiatype', $minfo['jiatype']);
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$jiatype.$minfo['duration'].'天';
        }

        // [申请人]的[流程名称] - [目的地]
        if($set['modenum'] == 'kao_chu'){
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$minfo['dest'];
        }

        // [申请人]的[流程名称] - [开始时间]
        if($set['modenum'] == 'kao_ban'){
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$minfo['startdt'];
        }

        // [申请人]的[流程名称] - [开始时间][目的地]
        if($set['modenum'] == 'kao_chai'){
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$minfo['startdt'].$minfo['dest'];
        }

        // [流程名称] - [工资标题][工资月份]
        if($set['modenum'] == 'wage'){
            $str = $set['name'].' - '.$minfo['title'].$minfo['month'];
        }

        // [申请人]的[流程名称]
        if($set['modenum'] == 'archive_diao'){
            $str = $minfo['created_user_name'].'的'.$set['name'];
        }

        // [申请人]的[流程名称]
        if($set['modenum'] == 'archive_zhuan'){
            $str = $minfo['created_user_name'].'的'.$set['name'];
        }

        // [申请人]的[流程名称]
        if($set['modenum'] == 'archive_out'){
            $str = $minfo['created_user_name'].'的'.$set['name'];
        }

        // 说明：员工入职审批 - 张三 - 综合部 - 2023-02-06     （人员姓名，人员所在部门，入职日期）
        if($set['modenum'] == 'cert_perru'){
            $deptName = DeptModel::where(['id'=>$minfo['deptid']])->value('name');
            $str = $set['name'].' - '.$minfo['name'].' - '.$deptName.' - '.$minfo['intodt'];
        }

        // 说明：招聘需求 - 需求部门 - 需求岗位 - 招聘人数
        if($set['modenum'] == 'cert_zhao'){
            $deptName = DeptModel::where(['id'=>$minfo['deptxu']])->value('name');
            $str = $set['name'].' - '.$deptName.' - '.$minfo['gangxu'].' - '.$minfo['zhaonum'];
        }

        // 离职交接 - 申请人 - 申请部门 - 交接时间 - 工作交接人
        if($set['modenum'] == 'archive_jiao'){
            $jiaoerName = UserModel::get_realname_by_pk($minfo['jiaoer']);
            $str = $set['name'].' - '.$minfo['created_user_name'].' - '.$minfo['dept'].' - '.$minfo['jiaodt'].' - '.$jiaoerName;
        }

        // 张三 - 通用审批流程 - 意见请示
        if($set['modenum'] == 'tongyong'){
            $shilei = (new OptService())->get_base_showname('qi_tongyong_shilei', $minfo['shilei']);
            $str = $minfo['created_user_name'].'的'.$set['name'].' - '.$shilei;
        }


        return $str;
    }


    public function create_flow_bidid($modenum, $minfo){
        $bidid = 0;
        if($modenum == 'bid'){
            $bidid = $minfo['id'];
        }
        if($modenum == 'bid_book'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'bid_grant_book'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'engin_bond_jiao'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'engin_bond_fan'){
            $shouinfo = EnginBondShouModel::where(['id'=>$minfo['shouid']])->find();
            if($shouinfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        if($modenum == 'engin_bond_kai'){
            if($minfo['bidid'] > 0){
                $bidid = $minfo['bidid'];
            }
        }
        return $bidid;
    }
    public function create_flow_enginid($modenum, $minfo){
        $enginid = 0;

        if($modenum == 'engin'){
            $enginid = $minfo['id'];
        }
        if($modenum == 'engin_bond_jiao'){
            if($minfo['enginid'] > 0){
                $enginid = $minfo['enginid'];
            }
        }
        if($modenum == 'engin_bond_fan'){
            $shouinfo = EnginBondShouModel::where(['id'=>$minfo['shouid']])->find();
            if($shouinfo['enginid'] > 0){
                $enginid = $shouinfo['enginid'];
            }
        }
        if($modenum == 'engin_bond_kai'){
            if($minfo['enginid'] > 0){
                $enginid = $minfo['enginid'];
            }
        }
        if($modenum == 'payment_bao'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'piao_kai'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'piao_tax'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'payment_neifu'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'payment_engin'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_junyan'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_junjie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'contract_shi'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'contract_bu'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'contract_cai'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'contract_bian'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_budget'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_budbian'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'stuff_cai'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'stuff_ru'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'stuff_jie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'labour_shen'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'labour_ling'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'labour_yong'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'labour_jie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'device_jin'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'device_jie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'device_lin'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'subbao_shen'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'subbao_jie'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_diary'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_speed'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_chan'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'engin_stamp'){
            $enginid = $minfo['enginid'];
        }
        if($modenum == 'visa'){
            $enginid = $minfo['enginid'];
        }
        return $enginid;
    }
    public function create_flow_enginame($modenum, $minfo){
        $id = $this->create_flow_bidid($modenum, $minfo);
        if($id > 0){
            return BidModel::where(['id'=>$id])->value('name');
        }else{
            $id = $this->create_flow_enginid($modenum, $minfo);
            if($id > 0){
                return EnginModel::where(['id'=>$id])->value('name');
            }
        }
        return '';
    }


    public function flow_task_comple_linkage($taskid, $minfo, $bill){
        $task = FlowTaskModel::where(['id'=>$taskid])->find();
        if(!$task){
            return;
        }

        if($task['opt'] == 'add'){//任务类型-加签
            if($task['addcate'] == 1){//被加签人会签
                $qiTask = $this->get_tongjiandson_is_weiwan($task);
                if(!$qiTask){
                    if($task['addtype'] == 1){//同意并加签
                        $this->flow_task_comple_linkage($task['pid'], $minfo, $bill);
                    }else{//直接加签  =====  上级任务启用
                        $pidTask = FlowTaskModel::where(['id'=>$task['pid']])->find();
                        FlowTaskModel::where(['id'=>$pidTask['id']])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                        (new WorkerService())->add_worker_bill($pidTask['uid'], 'shen', $bill);
                    }
                }

            }elseif($task['addcate'] == 2){//被加签人或签
                FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
                    ->where(['pid'=>$task['pid']])->update(['deleted_user'=>1]);
                if($task['addtype'] == 1){//同意并加签
                    $this->flow_task_comple_linkage($task['pid'], $minfo, $bill);
                }else{//直接加签  =====  上级任务启用
                    $pidTask = FlowTaskModel::where(['id'=>$task['pid']])->find();
                    FlowTaskModel::where(['id'=>$pidTask['id']])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                    (new WorkerService())->add_worker_bill($pidTask['uid'], 'shen', $bill);
                }
            }else{//被加签人依次审批
                $nextTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
                    ->where(['pid'=>$task['pid']])->where('id','gt',$task['id'])->find();
                if($nextTask){
                    FlowTaskModel::where(['id'=>$nextTask['id']])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                    (new WorkerService())->add_worker_bill($nextTask['uid'], 'shen', $bill);

                }else{
                    $this->flow_task_comple_linkage($task['pid'], $minfo, $bill);
                }
            }
        }elseif($task['opt'] == 'tui'){
            if($task['signtype'] == 1){//被加签人会签
                if($task['plain'] > 0){
                    $nextTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
                        ->where(['pid'=>$task['pid'],'deleted_user'=>0])->where('id','gt',$task['id'])->find();
                    if($nextTask){
                        FlowTaskModel::where(['id'=>$nextTask['id']])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                        (new WorkerService())->add_worker_bill($nextTask['uid'], 'shen', $bill);
                    }else{
                        if($task['tiaotype'] == 1){
                            FlowTaskModel::where('id','in',$task['tingids'])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                            $tingids_uids =  FlowTaskModel::where('id','in',$task['tingids'])->column('uid');
                            foreach($tingids_uids as $_uid){
                                (new WorkerService())->add_worker_bill($_uid, 'shen', $bill);
                            }
                        }else{
                            $delTask = FlowTaskModel::where(['billid'=>$task['billid'],'deleted_user'=>0])->column('id');
                            $dels = implode(',', $delTask);
                            FlowTaskModel::where('id','in',$delTask)->update(['deleted_user'=>1]);
                            //跳转下一节点
                            $nextNode = FlowNodeModel::where(['billid'=>$task['billid']])->where('id','gt',$task['nodeid'])->find();
                            $this->create_flow_node_task($nextNode, $minfo, $bill);
                        }
                    }
                }else{
                    $qiTask = $this->get_tongjiandson_is_weiwan($task);
                    if(!$qiTask){
                        if($task['tiaotype'] == 1){
                            FlowTaskModel::where('id','in',$task['tingids'])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                            $tingids_uids =  FlowTaskModel::where('id','in',$task['tingids'])->column('uid');
                            foreach($tingids_uids as $_uid){
                                (new WorkerService())->add_worker_bill($_uid, 'shen', $bill);
                            }
                        }else{
                            $delTask = FlowTaskModel::where(['billid'=>$task['billid'],'deleted_user'=>0])->column('id');
                            $dels = implode(',', $delTask);
                            FlowTaskModel::where('id','in',$delTask)->update(['deleted_user'=>1]);
                            //跳转下一节点
                            $nextNode = FlowNodeModel::where(['billid'=>$task['billid']])->where('id','gt',$task['nodeid'])->find();
                            $this->create_flow_node_task($nextNode, $minfo, $bill);
                        }
                    }
                }
            }
            if($task['signtype'] == 2){//被加签人或签
                $delTask = FlowTaskModel::where(['billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
                    ->where(['pid'=>$task['pid'],'deleted_user'=>0])->column('id');
                $dels = implode(',', $delTask);
                FlowTaskModel::where('id','in',$delTask)->update(['deleted_user'=>1]);


                if($task['tiaotype'] == 1){
                    FlowTaskModel::where('id','in',$task['tingids'])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                    $tingids_uids =  FlowTaskModel::where('id','in',$task['tingids'])->column('uid');
                    foreach($tingids_uids as $_uid){
                        (new WorkerService())->add_worker_bill($_uid, 'shen', $bill);
                    }
                }else{
                    $delTask = FlowTaskModel::where(['billid'=>$task['billid'],'deleted_user'=>0])->column('id');
                    $dels = implode(',', $delTask);
                    FlowTaskModel::where('id','in',$delTask)->update(['deleted_user'=>1]);
                    //跳转下一节点
                    $nextNode = FlowNodeModel::where(['billid'=>$task['billid']])->where('id','gt',$task['nodeid'])->find();
                    $this->create_flow_node_task($nextNode, $minfo, $bill);
                }
            }
        }else{
            if($task['signtype'] == 1){
                if($task['plain'] > 0){
                    $nextTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
                        ->where(['pid'=>$task['pid']])->where('id','gt',$task['id'])->find();
                    if($nextTask){
                        FlowTaskModel::where(['id'=>$nextTask['id']])->update(['state'=>1,'created_at'=>date("Y-m-d H:i:s")]);
                        (new WorkerService())->add_worker_bill($nextTask['uid'], 'shen', $bill);
                    }
                }
            }else{
                FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid']])->update(['deleted_user'=>1]);
            }
        }

        return;
    }





    public function get_tongjiandson_is_weiwan($task){
        $exist = false;
        $other = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid'],'nodeid'=>$task['nodeid']])
            ->where(['pid'=>$task['pid'],'deleted_user'=>0])
            ->where('id','neq', $task['id'])->select()->toArray();
        if(!$task){
            return $exist;
        }

        foreach ($other as $v){
            $exist = $this->check_benson_is_wan($v);
            if($exist){
                break;
            }
        }

        return $exist;
    }

    public function check_benson_is_wan($task){
        if($task['state'] != 2){
            return true;
        }

        $son = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$task['billid']])
            ->where(['nodeid'=>$task['nodeid'],'pid'=>$task['id'],'deleted_user'=>0])->select()->toArray();
        foreach ($son as $v){
            return $this->check_benson_is_wan($v);
        }

        return false;
    }


    public function add_flow_task_shen($billid, $nodeid, $taskid, $opt='', $name='', $recall=0, $promer='', $created_at=''){
        if(!empty($promer)){
            FlowPromerModel::insert([
                'comid' => $this->userinfo['comid'],
                'billid' => $billid,
                'uid' => $this->userinfo['id'],
                'domer' => $promer,
                'created_user' => $this->userinfo['id'],
                'created_at' => date("Y-m-d H:i:s"),
            ]);
        }
        return FlowTaskShenModel::insert([
            'comid' => $this->userinfo['comid'],
            'billid' => $billid,
            'nodeid' => $nodeid,
            'taskid' => $taskid,
            'uid' => $this->userinfo['id'],
            'opt' => $opt,
            'name' => $name,
            'recall' => $recall,
            'created_at' => $created_at,
            'updated_at' => date("Y-m-d H:i:s"),
        ]);
    }



    public function get_flow_flowtext($bill, $retype=0){
        $text = '';
        if($bill['isturn'] < 1){
            $text = '<span class="mindianhuang"></span>待申请人提交';
        }else{
            if($bill['status'] == 0){
                $task = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'state'=>1,'deleted_user'=>0])->select()->toArray();
                if(!$task){
                    $text = '<span class="mindianhong"></span>进行中(流程异常)';
                }else{
                    if(count($task) == 1 && $task[0]['nodeid'] == 0){
                        $text = '<span class="mindianhuang"></span>进行中(待申请人重新提交)';
                    }else{
                        $existName = '';
                        foreach ($task as $v){
                            $uname = UserModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'id'=>$v['uid']])->value('realname');
                            if($uname){
                                if(!$existName){
                                    $existName .= $uname;
                                }else{
                                    $existName .= ','.$uname;
                                }
                            }
                        }
                        if($existName){
                            $text = '<span class="mindianlan"></span>进行中(待 '.$existName.' 审核)';
                        }else{
                            $text = '<span class="mindianhong"></span>进行中(流程异常)';
                        }
                    }
                }
            }

            if($bill['status'] == 1){
                $text = '<span class="mindianlv"></span>审核完成(同意)';
            }

            if($bill['status'] == 2){
                $text = '<span class="mindianhong"></span>审核完成(不同意)';
            }

            if($bill['status'] == 3){
                $text = '<span class="mindianhong"></span>审核完成(超管结束)';
            }

            if($bill['status'] == 5){
                $text = '<span class="mindianhui"></span>审核完成(作废)';
            }
        }

        if($retype > 0){
            $text = strip_tags($text);
        }
        return $text;
    }

    public function get_flow_flowtext_strange($bill){
        $task = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'state'=>1,'deleted_user'=>0])->select()->toArray();
        if(!$task){
            return true;
        }else{
            $existName = '';
            foreach ($task as $v){
                $uname = UserModel::where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'id'=>$v['uid']])->value('realname');
                if($uname){
                    if(!$existName){
                        $existName .= $uname;
                    }else{
                        $existName .= ','.$uname;
                    }
                }
            }
            if(!$existName){
                return true;
            }
        }
        return false;
    }


    public function unset_flow_node_onshen($nodeid, $uid=0){
        if(!$uid){
            $uid = $this->userinfo['id'];
        }
        $old_onshen = FlowNodeModel::where(['id'=>$nodeid])->value('onshen');
        $old_onshen_arr = explode(',', $old_onshen);
        $uid_key = array_search($uid,$old_onshen_arr);
        if(isset($uid_key)){
            unset($old_onshen_arr[$uid_key]);
        }
        FlowNodeModel::where(['id'=>$nodeid])->update(['onshen'=>implode(',',$old_onshen_arr)]);
        return true;
    }



    public function check_isadvan($modenum){
        $res = false;
        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) return $res;

        $classid = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id'],'status'=>1])->value('id');
        if(!$classid) return $res;


        $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$classid,'status'=>1,'deleted_user'=>0])->find();
        if(!$course) return $res;

        $step = json_decode($course['step'], true);
        if(!is_array($step)) return $res;

        $isadvan = isset($step[0]['data']['isadvan']) ? $step[0]['data']['isadvan'] : 0;
        if($isadvan > 0){
            return true;
        }
        return $res;
    }

    public function check_isadvan_edit($modenum, $data){
        $res = false;

        if($data['isturn'] > 0) return $res;

        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) return $res;

        $classid = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id'],'status'=>1])->value('id');
        if(!$classid) return $res;


        $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$classid,'status'=>1,'deleted_user'=>0])->find();
        if(!$course) return $res;

        $step = json_decode($course['step'], true);
        if(!is_array($step)) return $res;

        $isadvan = isset($step[0]['data']['isadvan']) ? $step[0]['data']['isadvan'] : 0;
        if($isadvan > 0){
            return true;
        }
        return $res;
    }


    public function check_isnext($modenum){
        $res = false;
        $set = FlowSetModel::where(['modenum'=>$modenum])->find();
        if(!$set) return $res;

        $classid = FlowClassModel::where(['comid'=>$this->userinfo['comid'],'setid'=>$set['id'],'status'=>1])->value('id');
        if(!$classid) return $res;


        $course = FlowCourseModel::where(['comid'=>$this->userinfo['comid'],'classid'=>$classid,'status'=>1,'deleted_user'=>0])->find();
        if(!$course) return $res;

        $step = json_decode($course['step'], true);
        if(!is_array($step)) return $res;

        $isnext = isset($step[0]['data']['coursetype']) ? $step[0]['data']['coursetype'] : 0;
        if($isnext > 0){
            return true;
        }
        return $res;
    }


    public function get_flow_show_info($billid=0){
        $bill = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$billid])->find();
        if(!$bill){
            return [];
        }
        $bill['flowtext'] = $this->get_flow_flowtext($bill);

        $bill['but_edit'] = false;
        $bill['but_agree'] = false;
        $bill['but_agreeno'] = false;
        $bill['but_tui'] = false;
        $bill['but_add'] = false;
        $bill['but_zhuan'] = false;
        $bill['but_out'] = false;

        $data['isnext'] = 0;

        if($bill['isturn'] > 0 && $bill['status'] == 0 && $bill['deleted_user'] == 0){
            $myTask = FlowTaskModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id'],'state'=>1])
                ->where(['uid'=>$this->userinfo['id'],'deleted_user'=>0])->find();
            if($myTask){
                $myNode = FlowNodeModel::where(['id'=>$myTask['nodeid']])->find();
                $bill['but_zhuan'] = true;
                if($myTask['opt'] == 'tui' && $myTask['tuiid'] == 0 && $myTask['nodeid'] == 0){
                    $bill['but_edit'] = true;
                    $bill['but_edit_yushen'] = $myTask['tiaotype'];
                }else{
                    if($myNode['type'] == 2){
                        $data['isnext'] = 1;
                    }
                    $bill['but_agree'] = true;
                    $bill['but_agreeno'] = true;
                    $bill['but_tui'] = true;
                    $bill['but_add'] = $myTask['isadd'] > 0 ? true : false;
                    if($myTask['opt'] == 'add'){
                        $bill['but_out'] = true;
                    }
                }

                if($myNode['type'] == 2){
                    $bill['but_add'] = false;
                    $bill['but_zhuan'] = false;
                }
            }
        }

        $line = [];
        if($bill['isturn'] > 0){
            $line[0] = ['name' => '提交审批','state'=>0,'nodeid'=>0];
            $node = FlowNodeModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->select()->toArray();
            foreach ($node as $v){
                $line[] = ['name'=>$v['name'], 'state'=>0,'nodeid'=>$v['id']];
            }

            if($bill['status'] == 1){
                foreach ($line as &$item){
                    $item['state'] = 2;
                }
                $line[] = ['name' => '审核完成','state'=>2];
            }else{
                $curTaskNodeid = FlowTaskModel::where(['billid'=>$bill['id'],'state'=>1,'deleted_user'=>0])->order('nodeid ASC')->value('nodeid');
                foreach ($line as &$item){
                    if($item['nodeid'] < $curTaskNodeid){
                        $item['state'] = 2;
                    }elseif($item['nodeid'] == $curTaskNodeid){
                        $item['state'] = 1;
                        if(in_array($bill['status'], [2,3,5])){
                            $item['state'] = 2;
                        }
                    }
                }
                $line[] = ['name' => '审核完成','state'=>0];
            }
        }


        $shenlog = FlowShenlogModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->select()->toArray();
        foreach ($shenlog as &$item){
            $user = UserModel::field('realname,avatar')->where(['id'=>$item['created_user']])->find();
            $item['created_user'] = $user['realname'];
            $item['avatar'] = $user['avatar'];
            $item['filesarr'] = FileModel::field('name,encpath,ext')->where('id','in',$item['files'])->select()->toArray();
            $item['promer_name'] = UserModel::get_realname_by_ids($item['promer']);
        }

        $talk = FlowTalkModel::where(['comid'=>$this->userinfo['comid'],'billid'=>$bill['id']])->select()->toArray();
        foreach ($talk as &$item){
            $item['domer_name'] = UserModel::get_realname_by_ids($item['domer']);
            $item['uid_name'] = UserModel::get_realname_by_pk($item['uid']);
            $item['filesarr'] = FileModel::field('name,encpath,ext')->where('id','in',$item['files'])->select()->toArray();
            $item['isdel'] = $item['uid'] == $this->userinfo['id'] ? true : false;
        }

        $data['bill'] = $bill;
        $data['line'] = $line;
        $data['shenlog'] = $shenlog;
        $data['talk'] = $talk;
        $data['isqian'] = $this->cominfo['isqian'];


        $data['flowrelas_info'] = FlowBillModel::field('id,sericnum,modename,modenum,mid')->where('id','in',$bill['flowrelas'])->select()->toArray();
        return $data;
    }



    public function flow_bill_succ($bill){
        $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();

        if($bill['modenum'] == 'dealcom_geng'){
            (new OtherRelevan())->vate_dealcom($minfo);
        }

        if($bill['modenum'] == 'engin'){
            (new EnginRelevan())->vate_engin($minfo);
        }

        if($bill['modenum'] == 'engin_junyan'){
            (new EnginRelevan())->vate_engin_jundt($minfo);
        }

        if($bill['modenum'] == 'contract_shi'){
            (new EnginRelevan())->vate_engin_shimoney($minfo['enginid']);
        }

        if($bill['modenum'] == 'contract_bu'){
            (new EnginRelevan())->vate_engin_shimoney($minfo['enginid']);
        }

        if($bill['modenum'] == 'contract_bian'){
            if($minfo['isturn'] == 1 && $minfo['status'] == 1){
                (new EnginRelevan())->vate_contract_cai_list($minfo['id']);
            }
        }

        if($bill['modenum'] == 'engin_budbian'){
            if($minfo['isturn'] == 1 && $minfo['status'] == 1){
                (new EnginRelevan())->vate_engin_budget_list($minfo['id']);
            }
        }


        if($bill['modenum'] == 'cert_perru'){
            (new OtherRelevan())->vate_cert_perru_new($minfo);
        }

        if($bill['modenum'] == 'stuff_ru'){
            (new EnginRelevan())->vate_stuff_ru_create_chu($minfo);
        }

        if($bill['modenum'] == 'cert_use_bid'){
            (new CertRelevan())->set_cert_statueuse_bid_yong($minfo, $bill);
        }
        if($bill['modenum'] == 'cert_use'){
            (new CertRelevan())->set_cert_statueuse_engin_yong($minfo, $bill);
        }

        if($bill['modenum'] == 'cert_borrow'){
            (new CertRelevan())->set_cert_goes_yong($minfo, $bill);
        }

        if($minfo['isturn'] == 1 && $minfo['status'] == 1){
            $step = json_decode($bill['step'], true);
            if(isset($step[0]['data']['tonger']) && !empty($step[0]['data']['tonger'])){
                $mess = "有已通过的审批单待处理：".$bill['summary'];
                (new OptService())->add_todo($step[0]['data']['tonger'], $bill['modename'],$mess, $bill['mid'], $bill['modenum']);
            }
        }

    }
    public function flow_bill_fail($bill){
        $minfo = Db::name($bill['modenum'])->where(['id'=>$bill['mid']])->find();

        if($bill['modenum'] == 'bid'){
            (new CertRelevan())->set_cert_statueuse_bid($minfo['id']);
        }
        if($bill['modenum'] == 'engin'){
            (new CertRelevan())->set_cert_statueuse_engin($minfo['id']);
        }

        if($bill['modenum'] == 'cert_use_bid'){
            (new CertRelevan())->set_cert_statueuse_bid_shi($minfo, $bill);
        }
        if($bill['modenum'] == 'cert_use'){
            (new CertRelevan())->set_cert_statueuse_engin_shi($minfo, $bill);
        }

        if($bill['modenum'] == 'cert_borrow'){
            (new CertRelevan())->set_cert_goes_shi($minfo, $bill);
        }

    }



}
